package com.yiyuan.system.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.common.exception.SubRecExistException;
import com.yiyuan.system.model.Organ;
import com.yiyuan.system.object.TreeObj;
import com.yiyuan.system.service.ISystemService;

public class OrgAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/* ************* 参数封装******************************************** */

	private String id;

	private String orgId;
	private String orgPid;
	private String orgName;
	private String orgDuty;
	private String operation;
	private String orgCode;

	/* ************* 输出结果封装***************************************** */
	private Organ organ;
	private List<Organ> list;

	/* ************* 各功能函数******************************************* */
	// 资源引用
	@Resource
	private ISystemService systemService;

	// 进入机构主页
	public String index(){
		return "toMain";
	}

	// 获得机构列表
	public String list() {
		list = systemService.findOrgList();
		return "list";
	}

	// 保存机构
	public void save() throws IOException {
		Organ organ = null;
		String retFlag = null;
		try {
			if (this.operation != null && this.operation.equals("add")) {// 新增机构
				organ = new Organ();
				organ.setOrgName(this.orgName);
				organ.setOrgDuty(this.orgDuty);
				organ.setHasChild("0");
				organ.setOrgPid(Integer.valueOf(this.getOrgId()));
				organ.setOrgCode(this.getOrgCode());
				this.systemService.addOrgan(organ);
				retFlag = "add";
			} else if (this.operation != null
					&& this.operation.equals("update")) {// 修改机构
				organ = this.systemService.loadOrgan(Integer
						.valueOf(this.orgId));
				organ.setOrgName(this.orgName);
				organ.setOrgDuty(this.orgDuty);
				organ.setOrgCode(orgCode);
				this.systemService.updateOrgan(organ);
				retFlag = "update";
			} else {
				retFlag = "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			retFlag = "error";
		}

		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		ServletActionContext.getResponse().setContentType("text/xml; charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(retFlag);

	}

	// 获得机构详细
	public void load() {
		organ = systemService.loadOrgan(Integer.valueOf(id));
		JSONObject jso = new JSONObject(organ);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {

		}
	}

	// 查询机构树
	@SuppressWarnings("unchecked")
	public void listTree() {

		TreeObj treeObj = null;
		Organ organ = null;
		List<TreeObj> treeList = new ArrayList();
		if (id == null || id.equals("")) {
			id = "0";
		}
		list = systemService.findSubOrgList(id);
		for (int i = 0; i < list.size(); i++) {
			organ = list.get(i);
			treeObj = new TreeObj();
			treeObj.setId(organ.getOrgId().toString());
			treeObj.setPid(organ.getOrgPid().toString());
			treeObj.setText(organ.getOrgName());
			treeObj.setCheckstate(0);
			treeObj.setChildNodes(null);
			treeObj.setComplete(false);
			if (organ.getHasChild() != null && organ.getHasChild().equals("1")) {
				treeObj.setHasChildren(true);
			} else {
				treeObj.setHasChildren(false);
			}
			treeObj.setIsexpand(false);
			treeObj.setShowcheck(true);
			treeObj.setValue(organ.getOrgId().toString());
			treeList.add(treeObj);

		}
		Map map = new HashMap();
		map.put("list", treeList);
		JSONObject jso = new JSONObject(map);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {
		}
	}

	// 删除机构
	public void del() {
		String ret = null;
		try {
			this.systemService.deleteOrgan(Integer.valueOf(this.id));
			ret = "delete";
		} catch (SubRecExistException se) {
			ret = "exist";
		} catch (Exception e) {
			ret = "error";
		}

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(ret);
		} catch (Exception e) {

		}

	}

	/** **************************************** */

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getOrgPid() {
		return orgPid;
	}

	public void setOrgPid(String orgPid) {
		this.orgPid = orgPid;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getOrgDuty() {
		return orgDuty;
	}

	public void setOrgDuty(String orgDuty) {
		this.orgDuty = orgDuty;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}
	public List<Organ> getList() {
		return list;
	}

	public Organ getOrgan() {
		return organ;
	}

	public void setOrgan(Organ organ) {
		this.organ = organ;
	}
}
